package com.xinongbest.plugin;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;

import java.sql.Connection;
import java.util.Properties;

@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class IPlugin implements Interceptor {
    /**
     * 拦截方法：只要被拦截对象的对应方法被执行，则会执行该方法
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("自定义拦截方法执行了……");
        /** 让原方法执行 */
        return invocation.proceed();
    }

    /**
     * 主要是为了把当前的拦截器存储到代理生成的拦截器链中
     *
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        // Plugin.wrap(target,this);
        return Interceptor.super.plugin(target);
    }

    /**
     * 获取配置文件参数
     *
     * @param properties
     */
    @Override
    public void setProperties(Properties properties) {
        Interceptor.super.setProperties(properties);
    }
}
